package study_HttpClient;

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class HttpClient的Post方法带参数 {
    public static void main(String[] args) throws Exception{
        //1.创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //2.创建HttpPost对象，设置url访问地址
        HttpPost httpPost = new HttpPost("https://search.bilibili.com/all");

        //声明List集合，封装表单中的参数
        List<NameValuePair> params = new ArrayList<>();

        //设置请求地址是：https://search.bilibili.com/all?keyword=java
        params.add(new BasicNameValuePair("keyword","java"));

        //创建表单的Entity对象,第一个参数就是封装好的表单数据，第二个参数就是编码
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params,"utf8");

        //设置表单的Entity对象到post请求中
        httpPost.setEntity(formEntity);


        CloseableHttpResponse response = null;

        //3.使用HttpClient发起请求，获取response
        response = httpClient.execute(httpPost);

        //4.解析响应
        if (response.getStatusLine().getStatusCode() == 200){
            String content = EntityUtils.toString(response.getEntity(),"utf8");
            System.out.println(content.length());
        }

        //5.关闭response
        response.close();
        //6.关闭httpClient
        httpClient.close();
    }
}
